package com.sskj.finance.ui.fragment;


import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ToastUtil;
import com.sskj.finance.R;
import com.sskj.finance.R2;
import com.sskj.finance.bean.AccountBean;
import com.sskj.finance.bean.HomeLendGetTransrecord;
import com.sskj.finance.bean.RateBean;
import com.sskj.finance.bean.SupplyData;
import com.sskj.finance.presenter.FinanceFragmentPresenter;
import com.sskj.finance.util.TipFinanUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.TipUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.FINAN_FRAGMENT_FINANCE)//理财
public class FinanceFragment extends BaseFragment<FinanceFragmentPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private MVCCoolHelper<List<HomeLendGetTransrecord>> mvcHelper;
    private SlimAdapter slimAdapter;
    private int curPos;
    @Autowired
    String status;//1理财中2已完成/已赎回
    private BottomSheetDialog bottomSheetDialogPwd;


    @Override
    protected int getLayoutId() {
        return R.layout.finan_fragment_finance;
    }

    @Override
    public FinanceFragmentPresenter getPresenter() {
        return new FinanceFragmentPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        Map<String, String> statusMap = new HashMap<>();
        statusMap.put("1", App.INSTANCE.getString(R.string.finan_financeRecordActivity2_j));
        statusMap.put("2", App.INSTANCE.getString(R.string.finan_mortgageRecordActivity6_j));
        Map<String, String> is_autoMap = new HashMap<>();
        is_autoMap.put("0", App.INSTANCE.getString(R.string.finan_financeFragment3_j));
        is_autoMap.put("1", App.INSTANCE.getString(R.string.finan_financeFragment4_j));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.finan_recy_item_finance, new SlimInjector<HomeLendGetTransrecord>() {
            @Override
            public void onInject(HomeLendGetTransrecord data, IViewInjector injector, List payloads) {

                injector
                        .text(R.id.tvTime, data.getAddtime())
                        .text(R.id.tvFirstLeft, data.getPname())
                        .text(R.id.tvFirstRight, statusMap.get(data.getStatus()))
                        .text(R.id.tvSecondLeft, data.getNum() + data.getPname())
                        .text(R.id.tvSecondRight, data.getInterest_rate())
                        .text(R.id.tvThirdLeft, data.getDays())
                        .text(R.id.tvThirdRight, data.getDayth())
                        .text(R.id.tvFourLeft, is_autoMap.get(data.getIs_auto()))
                        .visibility(R.id.tvRedeem, data.getStatus().equals("1") ? View.VISIBLE : View.INVISIBLE)
                        .text(R.id.tvFourRight, data.getIncome() + data.getPname())
                        .clicked(R.id.tvRedeem, v -> {
                            curPos = slimAdapter.getData().indexOf(data);
//                            mPresenter.getRate(data.getTran_id());
                            RateBean rateBean = new RateBean();
                            rateBean.setFine_rate(data.getFine_rate());
                            showCancel(rateBean);

                        })

                ;
            }
        });
        slimAdapter.attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<HomeLendGetTransrecord> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<HomeLendGetTransrecord>)
                page -> mPresenter.homeLendGetTransrecord(status, page + "", "20"), 20);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }


    public void showCancel(RateBean rateBean) {
        HomeLendGetTransrecord dataItem = (HomeLendGetTransrecord) slimAdapter.getDataItem(curPos);
        TipUtil.getSureCancelTip(getActivity(), String.format(App.INSTANCE.getString(R.string.finan_financeFragment5_j),rateBean.getFine_rate()), () -> {
            bottomSheetDialogPwd = BottomSheetUtil.showPwd(getActivity(), str -> {
                bottomSheetDialogPwd.dismiss();

                mPresenter.homeLendPayBack(dataItem.getTran_id(),str[0]);

            });
            bottomSheetDialogPwd.show();
        }).show();
    }

    /**
     * 理财赎回成功
     */
    public void backSuccess() {
        mvcHelper.refresh();
    }


}
